﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Permission.Interface;
using System.Web.Mvc;
using Microhard.Permission.Models;

namespace Microhard.Permission_MembershipImpl
{
    public class PcdImpl : IPCD
    {
        public List<PCDMOdel> GetProvince()
        {
            MicrohardOUGDBDataContext db = new MicrohardOUGDBDataContext();
            var ps = db.Provinces.OrderBy(p => p.ProSort).ToList();
            return (from p in ps
                    select new PCDMOdel
                    {
                        Name = p.ProName,
                        Value = p.ProID.ToString(),
                        ParentValue = string.Empty
                    }).ToList();
        }

        public List<PCDMOdel> GetCity(int province)
        {
            MicrohardOUGDBDataContext db = new MicrohardOUGDBDataContext();
            IEnumerable<City> ps = null;
            if (province == -1)
            {
                ps = db.Cities.OrderBy(p => p.CitySort);
            }
            else
            {
                ps = db.Cities.Where(p => p.ProID == province).OrderBy(p => p.CitySort);
            }
            return (from p in ps
                    select new PCDMOdel
                    {
                        Name = p.CityName,
                        Value = p.CityID.ToString(),
                        ParentValue = p.ProID.Value.ToString()
                    }).ToList();
        }

        public List<PCDMOdel> GetDistince(int city)
        {
            MicrohardOUGDBDataContext db = new MicrohardOUGDBDataContext();
            IEnumerable<District> ps = null;
            if (city == -1)
            {
                ps = db.Districts.OrderBy(p => p.DisSort).ToList();
            }
            else
            {
                ps = db.Districts.Where(p => p.CityID == city).OrderBy(p => p.DisSort).ToList();
            }
            return (from p in ps
                    select new PCDMOdel
                    {
                        Name = p.DisName,
                        Value = p.Id.ToString(),
                        ParentValue = p.CityID.ToString()
                    }).ToList();
        }
    }
}
